package com.task.web.controller.core;

import javax.annotation.Resource;

import com.task.common.info.DeviceGroupInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.DeviceGroupRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.DeviceGroupService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:29
 */
@Api("")
@RestController
@RequestMapping("/deviceGroup")
public class DeviceGroupController extends BaseController {


  @Resource
  private DeviceGroupService deviceGroupService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('deviceGroup:list')")
  @GetMapping("/getDeviceGroupByPage")
  public TableDataInfo getDeviceGroupByPage(DeviceGroupRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(deviceGroupService.getDeviceGroupByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('deviceGroup:option')")
  @GetMapping("/getDeviceGroupForOption")
  public TableDataInfo getDeviceGroupForOption(DeviceGroupRequestDTO request) {
    return getDataTable(deviceGroupService.getDeviceGroupForOption(request));
  }

  @Log(title = "设备分组管理", businessType = BusinessType.INSERT)
  @PostMapping("/addDeviceGroup")
  public AjaxResult addDeviceGroup(@RequestBody DeviceGroupRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    deviceGroupService.addDeviceGroup(request);
    return success();
  }

  @Log(title = "设备分组管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyDeviceGroup")
  public AjaxResult modifyDeviceGroup(@RequestBody DeviceGroupRequestDTO request) {
    checkService.check("data_device_group", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    deviceGroupService.modifyDeviceGroup(request);
    return success();
  }

  @Log(title = "设备分组管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteDeviceGroup/{DeviceGroupCode}")
  public AjaxResult deleteDeviceGroup(@PathVariable String DeviceGroupCode) {
    checkService.check("data_device_group", DeviceGroupCode);
    deviceGroupService.deleteDeviceGroup(DeviceGroupCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }

  @GetMapping("/getDeviceGroupByCode/{deviceGroupCode}")
  @PreAuthorize("@ss.hasPermi('system:deviceGroup:edit')")
  public AjaxResult getDeviceGroupByCode(@PathVariable String deviceGroupCode) {
    checkService.check("data_device_group", deviceGroupCode);
    DeviceGroupInfo DeviceGroupInfo = deviceGroupService.getDeviceGroupDetailByCode(deviceGroupCode);
    return success(DeviceGroupInfo);
  }

}
